package com.daocao.hua.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.daocao.hua.R;
import com.daocao.hua.adapter.GridViewAddImgesAdpter;
import com.daocao.hua.api.Api;
import com.daocao.hua.api.bean.SpreadAddBean;
import com.daocao.hua.api.bean.UploadImageBean;
import com.daocao.hua.util.ImageUtil;
import com.daocao.hua.util.Network;
import com.daocao.hua.util.RxLifeUtil;
import com.daocao.hua.util.ToastUtil;
import com.daocao.hua.util.UI;
import com.daocao.hua.view.Dialog;
import com.daocao.hua.view.NestedGridView;
import com.daocao.hua.view.V19FrameLayout;
import com.daocao.hua.view.uploadImage.UploadImgAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3UploadThingsActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_content)
    EditText editContent;
    @BindView(R.id.btn_submit)
    SuperButton btnSubmit;
    @BindView(R.id.upload_gridview)
    NestedGridView uploadGridview;
    private Unbinder unbinder;
    private GridViewAddImgesAdpter gridViewAddImgesAdpter;
    private List<String> datas;
    private UploadImgAdapter uploadImgAdapter;
    private String id;
    private String type;
    private String show_tips;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_things);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        id = getIntent().getStringExtra("id");
        type = getIntent().getStringExtra("type");


        switch (type) {
            case "4":
                editName.setHint("请输入您的帖子链接或平台网址");
                show_tips = "帖子链接或平台网址不能为空";
                break;
            case "3":
                editName.setHint("请输入您的短视频链接或平台网址");
                show_tips = "短视频链接或平台网址不能为空";
                break;
            case "1":
                editName.setHint("请输入您推广的平台名称");
                show_tips = "推广的平台名称不能为空";
                break;
            case "2":
                editName.setHint("请输入您推广的平台名称");
                show_tips = "推广的平台名称不能为空";
                break;
            case "9":
                editName.setHint("请输入您的推广平台链接或平台网址");
                show_tips = "推广平台链接或平台网址不能为空";
                break;
        }

        txtTitle.setText(type);
        datas = new ArrayList<>();
        gridViewAddImgesAdpter = new GridViewAddImgesAdpter(datas, this);
        uploadGridview.setAdapter(gridViewAddImgesAdpter);
        uploadGridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                if (position == gridViewAddImgesAdpter.getCount() - 1 && position < 6 && !gridViewAddImgesAdpter.isLastUpload()) {
                    goToTakePhoto();
                }
            }
        });
    }

    public void goToTakePhoto() {

        checkPermission(new CheckPermListener() {
            @Override
            public void superPermission() {
                if (uploadImgAdapter == null) {
                    initUploadImgAdapter();
                }
                uploadImgAdapter.withMode(UploadImgAdapter.MODE_IMG).showDialog();
            }

            @Override
            public void superReject() {

            }
        }, R.string.camera_storage_card_p, R.string.get_cammra, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    private void initUploadImgAdapter() {
        uploadImgAdapter = new UploadImgAdapter(this, new UploadImgAdapter.Action2() {
            @Override
            public void call(Bitmap file, int mode) {
                if (mode == UploadImgAdapter.MODE_IMG) {
                    getUploadImg(file);
                }
            }
        });
    }

    public void getUploadImg(Bitmap bitmap) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        UploadImageBean bean = new UploadImageBean();
        bean.img = ImageUtil.Bitmap2StrByBase64(bitmap, 100);
        Api.api_service.uploadImg(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    savePath(result.getUrl());
                });
    }

    public void getSpreadAdd(String ftype, String title, String content, List<String> images) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        SpreadAddBean bean = new SpreadAddBean();
        bean.title = title;
        bean.ftype = ftype;
        bean.content = content;
        bean.images = images;
        Api.api_service.getSpreadAdd(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    Dialog.showExtensionSuccessDialog(V3UploadThingsActivity.this, new Dialog.DialogClickListener() {
                        @Override
                        public void confirm() {
                            Intent intent = new Intent(V3UploadThingsActivity.this, MyExtensionActivity.class);
                            intent.putExtra("is_shenhe", true);
                            startActivity(intent);
                            finish();
                        }
                    });
                });
    }

    public void savePath(String path) {
        gridViewAddImgesAdpter.getDatas().add(path);
        gridViewAddImgesAdpter.notifyDataSetChanged();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (uploadImgAdapter != null) {
            uploadImgAdapter.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_submit:
                if (TextUtils.isEmpty(UI.toString(editName))) {
                    ToastUtil.makeText(this, show_tips);
                    return;
                }
                if (TextUtils.isEmpty(UI.toString(editContent))) {
                    ToastUtil.makeText(this, "您说说的话不能为空哦");
                    return;
                }
                if (gridViewAddImgesAdpter.getDatas().size() == 0) {
                    ToastUtil.makeText(this, "相关截图不能为空");
                    return;
                }
                getSpreadAdd(id, UI.toString(editName), UI.toString(editContent), gridViewAddImgesAdpter.getDatas());
                break;
        }
    }
}
